#include "app_settings.h"
#include "app_about_os.h"
#include "app_main.h"
#include "app_mode_select.h"
#include "app_radio.h"
#include "app_ch_calib.h"
#include "app_ch_set.h"

#if (LV_USE_FS_WIN32 == 1)
#include "../hal/hal_link.h"
#elif (LV_USE_FS_POSIX == 1)
#include "hal_link.h"
#endif

// 全局变量，存储设置界面对象
static lv_obj_t *settings_screen;
static lv_style_t style_btn;
static lv_style_t style_label_bt;
static lv_style_t style_label2;

static void back_button_event_cb(lv_event_t *e);
static void mode_settings_button_event_cb(lv_event_t *e);
static void link_wifi_event_cb(lv_event_t *e);
static void radio_button_event_cb(lv_event_t *e);
static void play_video_event_cb(lv_event_t *e);
static void channel_calibration_button_event_cb(lv_event_t *e);
static void channel_set_button_event_cb(lv_event_t *e);
static void joystick_invert_button_event_cb(lv_event_t *e);
static void wifi_settings_button_event_cb(lv_event_t *e);
static void about_os_button_event_cb(lv_event_t *e);

static struct _settings_items_
{
    char *name;
    char *icon_pwd;
    void (*event_cb_fun)(lv_event_t *e) ;
} settings_items[] = {
    {"Mode Setting", "A:./img/set_list_icon/list_mode.png", mode_settings_button_event_cb},
    {"Channel Calibration", "A:./img/set_list_icon/list_calib.png", channel_calibration_button_event_cb},
    {"Channel Settings", "A:./img/set_list_icon/list_ch_set.png", channel_set_button_event_cb},
    {"Link Wifi", "A:./img/set_list_icon/list_wifi.png", link_wifi_event_cb},
    {"Radio", "A:./img/set_list_icon/list_radio.png", radio_button_event_cb},
    {"Play Video", "A:./img/set_list_icon/list_play_video.png", play_video_event_cb},
    // {"Joystick Invert", "A:./img/set_list_icon/list_mode.png", joystick_invert_button_event_cb},
    // {"WiFi Settings", "A:./img/set_list_icon/list_mode.png", wifi_settings_button_event_cb},
    {"About OS", "A:./img/set_list_icon/list_about.png", about_os_button_event_cb},
};

void back_to_set_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;
    
    hal_link->buzz_di();

    // 以淡入动画效果切换回设置界面
    lv_scr_load_anim(settings_screen, LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

void show_return_base(const char*name, lv_obj_t * parent, lv_event_cb_t event_cb)
{
    // 返回设置界面按钮
    lv_obj_t *back_button = lv_btn_create(parent);
    lv_obj_set_size(back_button, 50, 50);
    lv_obj_align(back_button, LV_ALIGN_TOP_LEFT, 30, 30);
    lv_obj_add_event_cb(back_button, event_cb, LV_EVENT_CLICKED, NULL);

    // 将样式应用到按钮
    lv_obj_add_style(back_button, &style_btn, 0);

    /* 创建一个标签并添加到按钮上 */
    lv_obj_t * label = lv_label_create(back_button);
    lv_label_set_text(label, LV_SYMBOL_LEFT);
    lv_obj_add_style(label, &style_label_bt, 0);

    /* 标题 */
    lv_obj_t *title_label = lv_label_create(parent);
    lv_label_set_text(title_label, name);
    lv_obj_align_to(title_label, back_button, LV_ALIGN_OUT_RIGHT_MID, 0, -2);
    lv_obj_add_style(title_label, &style_label2, 0);
}

// 返回主界面按钮点击事件处理函数
static void back_button_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    hal_link->buzz_di();

    // 以淡入动画效果切换回主界面
    lv_scr_load_anim(main_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

static void link_wifi_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;
}

// 跳转到模式设置界面事件处理函数
static void mode_settings_button_event_cb(lv_event_t *e) 
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    hal_link->buzz_di();

    // 以淡入动画效果切换到模式设置界面
    lv_scr_load_anim(mode_settings_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

// 跳转到通道校准界面事件处理函数
static void channel_calibration_button_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    hal_link->buzz_di();

    // 以淡入动画效果切换到通道校准界面
    lv_scr_load_anim(ch_calib_screen_get_have_init(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

// 跳转到通道校准界面事件处理函数
static void channel_set_button_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    hal_link->buzz_di();

    // 以淡入动画效果切换到通道校准界面
    lv_scr_load_anim(ch_set_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

static void radio_button_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    hal_link->buzz_di();

    // 以淡入动画效果切换到通道校准界面
    lv_scr_load_anim(radio_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

static void play_video_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;
    // lv_scr_load_anim(radio_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
}

// 跳转到摇杆取反界面事件处理函数
static void joystick_invert_button_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;
    // 以淡入动画效果切换到摇杆取反界面
    // lv_scr_load_anim(joystick_invert_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
}

// 跳转到无线设置界面事件处理函数
static void wifi_settings_button_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;
    // 以淡入动画效果切换到无线设置界面
    // lv_scr_load_anim(wifi_settings_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, false);
}

// 跳转到 About OS 界面事件处理函数
static void about_os_button_event_cb(lv_event_t *e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;
    
    hal_link->buzz_di();

    // 以淡入动画效果切换到 About OS 界面
    lv_scr_load_anim(about_os_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

static void return_bt_init(void)
{
    /* 设置按钮样式为透明背景 */
    lv_style_init(&style_btn);
    lv_style_set_radius(&style_btn, 10); // 设置按钮样式圆角弧度
    
    // 设置背景完全透明
    lv_style_set_bg_opa(&style_btn, LV_OPA_TRANSP);
    lv_style_set_border_opa(&style_btn, LV_OPA_TRANSP);
    lv_style_set_shadow_opa(&style_btn, LV_OPA_TRANSP);

    // 初始化标签样式
    lv_style_init(&style_label_bt);
    lv_style_set_text_color(&style_label_bt, lv_color_black());
    lv_style_set_text_font(&style_label_bt, &lv_font_montserrat_26);
    
    lv_style_init(&style_label2);
    lv_style_set_text_font(&style_label2, &lv_font_montserrat_18);
}

// 创建设置界面
void settings_screen_init(void)
{
    settings_screen = lv_obj_create(NULL);

    return_bt_init();
    
    lv_obj_t *title_label = lv_label_create(settings_screen);
    lv_label_set_text(title_label, "Settings Menu");
    lv_obj_align(title_label, LV_ALIGN_TOP_MID, 0, 20);

    // 返回主界面按钮
    lv_obj_t *back_button = lv_btn_create(settings_screen);
    lv_obj_align(back_button, LV_ALIGN_TOP_LEFT, 20, 20);
    lv_obj_add_event_cb(back_button, back_button_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *back_label = lv_label_create(back_button);
    lv_label_set_text(back_label, "返回");


    // 初始化标签样式
    static lv_style_t style_label;
    lv_style_init(&style_label);

    // 设置标签文本颜色为白色
    lv_style_set_text_color(&style_label, lv_color_white());
    lv_style_set_text_font(&style_label, &font_22_alibaba);
    lv_obj_add_style(back_label, &style_label, 0);

    lv_obj_t *list = lv_list_create(settings_screen);
    lv_obj_set_size(list, lv_pct(80), lv_pct(70));
    lv_obj_align(list, LV_ALIGN_CENTER, 0, 0);

    for (int i = 0; i < sizeof(settings_items) / sizeof(struct _settings_items_); i++) {
        lv_obj_t *list_btn = lv_list_add_btn(list, settings_items[i].icon_pwd, settings_items[i].name);
        lv_obj_add_event_cb(list_btn, settings_items[i].event_cb_fun, LV_EVENT_CLICKED, NULL);
    }
}

bool is_screen_active(lv_obj_t *is_screen)
{
    lv_obj_t * active_screen = lv_scr_act();
    return active_screen == is_screen;
}

// 获取设置界面对象
lv_obj_t *settings_screen_get(void)
{
    return settings_screen;
}

/******************* (C) COPYRIGHT 2025 FUYEBING*****END OF FILE************/
